#pragma once
#include"reverse_iterator.h"

namespace cyq
{
	template<class T>
	class list_node
	{
	public:
		list_node(const T& x=T())
			:_val(x)
			,_next(nullptr)
			,_prev(nullptr)
		{}

		T _val;
		list_node<T>* _next;
		list_node<T>* _prev;
	};

	template<class T,class Ref,class Ptr>
	class list_iterator
	{
	public:
		typedef list_node<T> Node;
		typedef list_iterator<T, Ref, Ptr> Self;
		Node* _node;

		list_iterator(Node* node)
			:_node(node)
		{}

		Ref operator*()
		{
			return _node->_val;
		}

		Ptr operator->()
		{
			return &(_node->_val);
		}

		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		Self operator++(int)
		{
			list_iterator temp(*this);
			_node = _node->_next;
			return temp;
		}

		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		Self operator--(int)
		{
			list_iterator temp(*this);
			_node = _node->_prev;
			return temp;
		}

		bool operator!=(const Self& lt) const
		{
			return _node != lt._node;
		}

		bool operator==(const Self& lt) const
		{
			return _node == lt._node;
		}
	};

	template<class T>
	class list
	{
	public:
		typedef list_node<T> Node;
		typedef list_iterator<T, T&, T*> iterator;
		typedef list_iterator<T, const T&,const T*> const_iterator;
		typedef cyq::reverse_iterator< T&, T*, iterator> reverse_iterator;
		typedef cyq::reverse_iterator< const T&, const T*, const_iterator> const_reverse_iterator;


		iterator begin()
		{
			return _head->_next;
		}

		iterator end()
		{
			return _head;
		}

		const_iterator begin() const
		{
			return _head->_next;
		}

		const_iterator end() const
		{
			return _head;
		}

		reverse_iterator rbegin()
		{
			return end();
		}

		reverse_iterator rend()
		{
			return begin();
		}

		/*reverse_iterator rbegin()
		{
			return reverse_iterator(end());
		}

		reverse_iterator rend()
		{
			return reverse_iterator(begin());
		}*/

		const_reverse_iterator rbegin()const
		{
			return end();
		}

		const_reverse_iterator rend() const
		{
			return begin();
		}

		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;

			_size = 0;
		}

		list()
		{
			empty_init();
		}

		list(const list<T>& lt)
		{
			empty_init();
			for (auto e : lt)
			{
				pusj_back(e);
			}
		}

		void swap(const list<T>& lt)
		{
		    std::swap(_head, lt._head);
			std::swap(_size, lt.size);
		}

		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
				it++;
			}
			_size = 0;
		}

		iterator insert(iterator pos, const T& x = T())
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* newnode = new Node(x);

			prev->_next = newnode;
			newnode->_prev = prev;
			cur->_prev = newnode;
			newnode->_next = cur;

			_size++;

			return newnode;
		}

		void push_back(const T& x = T())
		{
			insert(_head, x);
		}

		iterator erase(iterator pos)
		{

			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;

			prev->_next = next;
			next->_prev = prev;
			delete cur;
			cur = nullptr;

			_size--;

			return next;
		}

		void pop_back()
		{
			erase(--end());
		}

		void push_front(const T& x = T())
		{
			insert(begin(), x);
		}

		void pop_front()
		{
			erase(begin());
		}

		bool empty()
		{
			return _size == 0;
		}

		size_t size()
		{
			return _size;
		}



	private:
		Node* _head;
		size_t _size;
	};
}
